package com.example.sqliteprueba;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class SQLAdmin extends SQLiteOpenHelper {
	// Nombre de la base de datos
	private static final String DATABASE_NAME = "sqliteprueba.db";
	// Nombre de la version
	private static final int DATABASE_VERSION = 5;
	// Nombre de la tabla
	public static final String TABLE_CARGAS = "cargas";
	// Nombre de las columnas
	public static final String COLUMN_ID = "_id";
	public static final String COLUMN_FECHA = "fecha";
	public static final String COLUMN_KMS = "kms";
	public static final String COLUMN_PRECIO = "precio";
	public static final String COLUMN_LITROS = "litros";
	public static final String COLUMN_TANQUE_LLENO = "tanqueLleno";
	
	//Instancia estatica para implementar Singleton
	private static SQLAdmin sInstance;

	public static SQLAdmin getInstance(Context context) {

		// Use the application context, which will ensure that you
		// don't accidentally leak an Activity's context.
		// See this article for more information: http://bit.ly/6LRzfx
		if (sInstance == null) {
			sInstance = new SQLAdmin(context.getApplicationContext());
		}
		return sInstance;
	}

	// String del script CREATE TABLE
	private static final String DATABASE_CREATE ="create table "+ TABLE_CARGAS + "(" 
			+ COLUMN_ID	+ " integer primary key autoincrement, " 
			+ COLUMN_FECHA	+ " text not null," 
			+ COLUMN_KMS + " real not null, "
			+ COLUMN_PRECIO + " real not null, " 
			+ COLUMN_LITROS	+ " real not null, "
			+ COLUMN_TANQUE_LLENO+" BOOLEAN NOT NULL DEFAULT 0"
			+");";

	// public SQLAdmin(Context context, String name, CursorFactory factory,
	// int version) {
	// //super(context, name, factory, version);
	// super(context, DATABASE_NAME, null, DATABASE_VERSION);
	// // TODO Auto-generated constructor stub
	// }
	public SQLAdmin(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		db.execSQL(DATABASE_CREATE);

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		Log.w(SQLAdmin.class.getName(), "Upgrading database from version "
				+ oldVersion + " to " + newVersion
				+ ", which will destroy all old data");
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_CARGAS);
		onCreate(db);
	}

	@Override
	public SQLiteDatabase getReadableDatabase() {
		// TODO Auto-generated method stub
		return super.getReadableDatabase();
	}

	@Override
	public SQLiteDatabase getWritableDatabase() {
		// TODO Auto-generated method stub
		return super.getWritableDatabase();
	}

}
